package web.eshop;

import org.apache.wicket.extensions.markup.html.captcha.CaptchaImageResource;

import java.io.Serializable;

public class CommentBean implements Serializable {

    private String name;
    private String email;
    private String confirmEmail;
    private String comment;
    private String password;
    private String imagePass = randomString(2, 3);
    private CaptchaImageResource captchaImage = new CaptchaImageResource(imagePass);

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CaptchaImageResource getCaptchaImage() {
        return captchaImage;
    }

    public void setCaptchaImage(CaptchaImageResource captchaImage) {
        this.captchaImage = captchaImage;
    }

    public String getImagePass() {
        return imagePass;
    }

    public void setImagePass(String imagePass) {
        this.imagePass = imagePass;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getConfirmEmail() {
        return confirmEmail;
    }

    public void setConfirmEmail(String confirmEmail) {
        this.confirmEmail = confirmEmail;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void clear() {
        this.name = null;
//        this.email = null;
//        this.confirmEmail = null;
        this.comment = null;
        this.password = null;
        this.imagePass = randomString(2, 3);
        this.captchaImage = new CaptchaImageResource(imagePass);
    }

    private static int randomInt(int min, int max) {
        return (int) (Math.random() * (max - min) + min);
    }

    private static String randomString(int min, int max) {
        int num = randomInt(min, max);
        byte b[] = new byte[num];
        for (int i = 0; i < num; i++) {
            b[i] = (byte) randomInt('a', 'z');
        }
        return new String(b);
    }

    public void invalidate() {
        captchaImage.invalidate();
        this.password = null;
        this.imagePass = randomString(2, 3);
        this.captchaImage = new CaptchaImageResource(imagePass);
    }

    public boolean passCheck() {
        if (imagePass.equalsIgnoreCase(password)) {
            return true;

        }
        return false;
    }
}
